package test.service;


import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.system.db.TransactionManager;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.GenerateID;
import m.system.util.SystemUtil;
import test.model.SchoolRoom;
import test.model.StudentInfo;
import test.model.UserInfo;

import java.sql.SQLException;

/**
 * Service实例需继承m.common.service.Service
 * Service实例为单例模式，尽量不要使用私有变量
 * 在action和service里可以调用
 *   -- getService(InterService.class).方法
 */
public class InterService extends Service {
    public void save(UserInfo user) throws Exception {
        SystemUtil.println(user);
        //事务写法--------
        TransactionManager tm=new TransactionManager();
        try{
            tm.begin();//事务开始

            //查询
            SchoolRoom room=getModel(SchoolRoom.class,new String[]{"*"},
                    QueryCondition.eq("grade",SchoolRoom.Grade1));
            if(null==room) throw new MException("没有找到班级");

            StudentInfo student=new StudentInfo();
            student.setOid(GenerateID.generatePrimaryKey());//生成左键
            student.setRoom(room);
            student.setName("张三");
            student.setBirthday(DateUtil.format("2015-02-18","yyyy-MM-dd"));
            //插入
            insertModel(student);
    //        updateModel() //更新
    //        deleteModel() //删除

        //事务写法-------
            tm.commit();//事务提交
        }catch (Exception e){
            tm.rollback();//事务回滚
            throw e;
        }
    }
}
